package com.yonyou.pmclouds.plan.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.impl.NCDepartmentQueryService;
import com.yonyou.pmclouds.organization.entity.OrganizationVO;
import com.yonyou.pmclouds.organization.rmiitf.OrganizationQuery;
import com.yonyou.pmclouds.plan.plandriven.entity.NcDepartmentVO;
import com.yonyou.pmclouds.plan.plandriven.entity.NcDeptQueryParamVO;
import com.yonyou.pmclouds.plan.plandriven.rmiitf.IPlanDepartmemtQuery;
import com.yonyou.pmclouds.project.entity.ProjectVO;
import com.yonyou.pmclouds.project.rmiitf.ProjectSimpleQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author th
 * @date 2019/11/26 13:37
 */
@Slf4j
@Service(interfaceClass = IPlanDepartmemtQuery.class, version = ApplicationConsts.APPLICATION_VERSION)
public class PlanDepartmentQueryImpl implements IPlanDepartmemtQuery {
    @Autowired
    private NCDepartmentQueryService ncDepartmentQueryService;
    @Autowired
    private ProjectSimpleQuery projectSimpleQuery;
    @Autowired
    private OrganizationQuery organizationQuery;

    @Override
    public List<NcDepartmentVO> queryNcDepartment(NcDeptQueryParamVO paramVO) throws BusinessException {
        if (StringUtils.isEmpty(paramVO.getPkOrg()) && StringUtils.isEmpty(paramVO.getPkProject()) &&
                CollectionUtils.isEmpty(paramVO.getPkDepts()))
            return new ArrayList<>();

        if (StringUtils.isNotEmpty(paramVO.getPkProject())) {
            ProjectVO projectVO = projectSimpleQuery.queryProject(paramVO.getPkProject());
            // 判断是否NC同步项目
            if (projectVO != null && StringUtils.isNotEmpty(projectVO.getPkSrc()))
                // 设置组织
                paramVO.setPkOrg(projectVO.getPkOrg());
        } else if (StringUtils.isNotEmpty(paramVO.getPkOrg())) {
            OrganizationVO organizationVO = organizationQuery.queryById(paramVO.getPkOrg());
            if (organizationVO != null)
                paramVO.setPkOrg(organizationVO.getPkSrc());
        }

        List<NcDepartmentVO> departmentVOs = ncDepartmentQueryService.queryDepartmentFromNc(paramVO);
        // 翻译部门的话不需要构建树
        if (CollectionUtils.isNotEmpty(paramVO.getPkDepts()))
            return departmentVOs;

        return buildTree(departmentVOs);
    }

    /**
     * 将部门构造树返回
     *
     * @param allVOs
     * @return
     */
    private List<NcDepartmentVO> buildTree(List<NcDepartmentVO> allVOs) {
        if (CollectionUtils.isEmpty(allVOs)) {
            return allVOs;
        }
        // 构造树
        Map<String, List<NcDepartmentVO>> deptChildMap = new HashMap<>();
        Map<String, String> deptCodeMap = new HashMap<>();//主键编码映射
        for (NcDepartmentVO vo : allVOs) {
            deptCodeMap.put(vo.getPkDept(), vo.getCode());
        }

        for (NcDepartmentVO vo : allVOs) {
            String parent = vo.getPkParent();
            if (StringUtils.isEmpty(parent)) {
                parent = StringUtils.EMPTY;
            } else {
                //前端需要子节点中存放父节点的编码
                vo.setParentCode(deptCodeMap.get(parent));
            }

            List<NcDepartmentVO> children = deptChildMap.get(parent);
            if (children == null) {
                children = new ArrayList<>();
                deptChildMap.put(parent, children);
            }

            children.add(vo);
        }

        for (NcDepartmentVO vo : allVOs) {
            List<NcDepartmentVO> children = deptChildMap.get(vo.getPkDept());
            vo.setChildren(children);
        }

        List<NcDepartmentVO> result = deptChildMap.get(StringUtils.EMPTY);
        if (result == null) {
            return new ArrayList<>();
        } else {
            return result;
        }
    }
}
